﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;
using Yglu.BusinessLayer.Entities;
using Yglu.BusinessLayer.Enums;

namespace Yglu.WebAdmin
{
	public partial class MainLayout_master : MasterPage
	{
		#region Main methods

		protected void Page_Load(Object sender, EventArgs e)
		{
			LitTitle.Text = Settings.SystemConfig.CompanyName;
			LblError.Text = String.Empty;

			if (SessionManager.LoggedUser.AdminLevel < AdminLevel.Company)
			{
				CboStores.Visible = false;
			}
			else if (CboStores.Items.Count < 1)
			{
				CboStores.DataSource = Settings.Stores;
				CboStores.DataBind();

				CboStores.Items.Insert(0, new ListItem(Messages.AllStores, "0"));
                CboStores.SelectedValue = SessionManager.LoggedStore.Id.ToString();
			}
		}

		protected void Page_PreRender(Object sender, EventArgs e)
		{
			LitFooterNotice.Text = LitFooterNotice.Text.Replace("#EMAIL#", Settings.SystemConfig.GeneralEmail);
			LitFooterNotice.Text = LitFooterNotice.Text.Replace("#PHONE#", Settings.SystemConfig.GeneralPhone);

			LitTitle.Text = Page.Title;

			PanError.Visible = !String.IsNullOrEmpty(LblError.Text);
		}

		public void SetError(Exception ex, String message)
		{
			ErrorManager.Log(ex, message);

			LblError.Text = ex.ToString();

			Page.ClientScript.RegisterStartupScript(typeof(String), "MasterError", "YgluTopAlert('" + message + "', false);", true);
		}

		#endregion

		#region Store selection

		protected void CboStores_SelectedIndexChanged(Object sender, EventArgs e)
		{
			if (CboStores.SelectedValue != "0")
			{
				SessionManager.LoggedStore = new Store(Convert.ToInt32(CboStores.SelectedValue));
			}
			else
			{
				SessionManager.LoggedStore = new Store();
			}
		}

		#endregion

	}
}